<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="com.sqlConnection.MyConnection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Address Book</title>
<style>
header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px;	 
}
nav {
    line-height:30px;
    background-color:#eeeeee;
    height:100%;
    width:100px;
    bottom: 0;
    float:left;
    padding:5px;	      
}
section {
    width:350px;
    float:left;
    padding:10px;		 
}
footer {
	width:100%;
	height:30px;
	position:absolute;
	bottom:0;
	left:0;
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
    padding:5px;	 	 
}

#mybutton {
	width: 30px;  height: 30px;
}

</style>
</head>
<body>
<header>
<h1>ADDRESS BOOK</h1>
</header>

<nav>
Best<br/>
Address<br/>
Book<br/>
Of<br/>
The<br/>
Universe<br/>
(Sarcasm!))
</nav>

<section>
	<table>
		<tr>
			<th>Name</th>
			<th>Surname</th>
			<th>Mail</th>
			<th>Address</th>
		</tr>
		<%	
		Statement statement = MyConnection.getStatement();
		if("delete".equals(request.getParameter("action"))){
			String id =request.getParameter("id");
			if(id != null){
				statement.executeUpdate("DELETE FROM ADDRESS_BOOK WHERE ID="+id);
			}
		} else if("update".equals(request.getParameter("action"))){
			String id =request.getParameter("id");
			String name = request.getParameter("name");
			String surname = request.getParameter("surname");
			String mail = request.getParameter("mail");
			String address = request.getParameter("address");
			if(name!= null && !name.isEmpty()){
				String query = "UPDATE ADDRESS_BOOK SET NAME='"+name+"', SURNAME='" +surname+"', MAIL='"+mail+"', ADDRESS='"+address+"' WHERE ID="+id;
				statement.executeUpdate(query);
			}
		}else {
			String id =request.getParameter("id");
			String name = request.getParameter("name");
			String surname = request.getParameter("surname");
			String mail = request.getParameter("mail");
			String address = request.getParameter("address");
			if(name!= null && !name.isEmpty()){
				String query = "INSERT INTO ADDRESS_BOOK (NAME, SURNAME, MAIL, ADDRESS) VALUES ('"+name+"', '"+surname+"', '"+mail+"','"+address+"')";
				statement.executeUpdate(query);
			}
		}	
		
		ResultSet rs = statement.executeQuery("SELECT * FROM ADDRESS_BOOK ORDER BY ID"); 
		while(rs.next()){
		%>
		<tr>
			<td><%=rs.getString("NAME")%></td>
			<td><%=rs.getString("SURNAME")%></td>
			<td><%=rs.getString("MAIL")%></td>
			<td><%=rs.getString("ADDRESS")%></td>
			<td>
				<form action="person.jsp" method="get">
					<input type="image" id="mybutton" src="D:\WorkSpace\AddressBookPractice\lib\update.png" alt="submit form"></input>
					<input type="hidden" name="name" value="<%=rs.getString("NAME") %>"></input>
					<input type="hidden" name="surname" value="<%=rs.getString("SURNAME") %>"></input>
					<input type="hidden" name="mail" value="<%=rs.getString("MAIL") %>"></input>
					<input type="hidden" name="address" value="<%=rs.getString("ADDRESS") %>"></input>
					<input type="hidden" name="id" value="<%=rs.getString("ID") %>"></input>
					<input type="hidden" name="action" value="update"></input>
				</form>
			</td>
			<td>
				<form action="" method="get">
					<input type="image" id="mybutton" src="D:\WorkSpace\AddressBookPractice\lib\delete.jpg" alt="submit form"></input>
					<input type="hidden" name="id" value="<%=rs.getString("ID") %>"></input>
					<input type="hidden" name="action" value="delete"></input>
				</form>
			<td>
		</tr>	
		<% } %>
		<tr>
			<form action="person.jsp" method="get">
					<input type="submit" value="NEW ENTRY"></input>
					<input type="hidden" name="name" value=""></input>
					<input type="hidden" name="surname" value=""></input>
					<input type="hidden" name="mail" value=""></input>
					<input type="hidden" name="address" value=""></input>
					<input type="hidden" name="action" value="new"></input>
				</form>
		</tr>
	</table>
</section>

<footer>
Cool Address Book
</footer>

</body>
</html>